﻿<template>
    <div class="SHZY-common-layout">
        <div class="SHZY-common-layout-center">
            <el-row class="SHZY-common-search-box" :gutter="16">
                <el-form @submit.native.prevent="">
                    <el-col :span="6">
                        <el-form-item label="工单号">
                            <el-input v-model="query.code" placeholder="工单号" clearable />
                        </el-form-item>
                    </el-col>
                    <el-col :span="6">
                        <el-form-item label="工单类型">
                            <el-select v-model='query.type' placeholder='工单类型' clearable>
                                <el-option v-for="(item, index) in workOrderTypeData" :key="index" :label="item.fullName"
                                    :value="item.enCode"></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="6">
                        <el-form-item label="状态">
                            <el-select v-model='query.status' placeholder='状态' clearable>
                                <el-option v-for="(item, index) in statusData" :key="index" :label="item.fullName"
                                    :value="item.enCode"></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="6">
                        <el-form-item>
                            <el-button type="primary" icon="el-icon-search" @click="search()">查询</el-button>
                            <el-button icon="el-icon-refresh-right" @click="reset()">重置</el-button>
                        </el-form-item>
                    </el-col>
                </el-form>
            </el-row>
            <div class="SHZY-common-layout-main SHZY-flex-main">
                <div class="SHZY-common-head">
                    <div>
                        <el-button type="primary" icon="el-icon-plus" @click="addOrUpdateHandle()">新增</el-button>
                        <el-button type="text" icon="el-icon-download" @click="exportData()">导出</el-button>
                        <el-button type="text" icon="el-icon-delete" @click="handleBatchRemoveDel()">批量删除</el-button>
                    </div>
                    <div class="SHZY-common-head-right">
                        <el-tooltip effect="dark" content="刷新" placement="top">
                            <el-link icon="icon-ym icon-ym-Refresh SHZY-common-head-icon" :underline="false"
                                @click="reset()" />
                        </el-tooltip>
                    </div>
                </div>
                <SHZY-table v-loading="listLoading" :data="list" @sort-change='sortChange' has-c
                    @selection-change='handleSelectionChange' :selectable="selectHandle" custom-column>
                    <el-table-column label="工单号" prop="code" align="left" width="150" sortable='custom'>
                    </el-table-column>
                    <el-table-column label="状态" prop="status" width="150" align="left">
                        <template slot-scope="scope">
                            <el-tag v-if="scope.row.status == 0" type="info">未确认</el-tag>
                            <el-tag v-if="scope.row.status == 1">已提交</el-tag>
                            <el-tag v-if="scope.row.status == 2" type="success">已完工</el-tag>
                        </template>
                    </el-table-column>
                    <el-table-column label="工单类型" prop="type" align="left" width="120" sortable='custom'>
                        <template slot-scope="scope">
                            {{ getWorkOrderTypeDes(scope.row.type) }}
                        </template>
                    </el-table-column>
                    <el-table-column label="物料" prop="materialId" align="left" width="250" :show-overflow-tooltip="true"
                        sortable='custom'>
                    </el-table-column>
                    <el-table-column label="数量" prop="qty" align="left" width="100" sortable='custom'>
                    </el-table-column>
                    <el-table-column label="单位" prop="unit" align="left" width="100" sortable='custom'>
                        <template slot-scope="scope">
                            {{ getUnitDes(scope.row.unit) }}
                        </template>
                    </el-table-column>
                    <el-table-column label="计划开始时间" prop="planStartTime" width="150" align="left" sortable='custom'>
                        <template slot-scope="scope">{{ scope.row.planStartTime | toDate() }}</template>
                    </el-table-column>
                    <el-table-column label="计划完成时间" prop="planEndTime" width="150" align="left" sortable='custom'>
                        <template slot-scope="scope">{{ scope.row.planEndTime | toDate() }}</template>
                    </el-table-column>
                    <el-table-column label="实际开始时间" prop="actualStartTime" width="150" align="left">
                        <template slot-scope="scope">{{ scope.row.actualStartTime | toDate() }}</template>
                    </el-table-column>
                    <el-table-column label="实际完成时间" prop="actualEndTime" width="150" align="left">
                        <template slot-scope="scope">{{ scope.row.actualEndTime | toDate() }}</template>
                    </el-table-column>
                    <el-table-column label="报工明细" prop="detail" width="150" align="left">
                        <template slot-scope="scope">
                            <el-button icon="el-icon-search" @click="opendDetail(scope.row.id)"
                                :disabled="scope.row.status == 0">报工明细</el-button>
                        </template>
                    </el-table-column>
                    <el-table-column label="备注" prop="remark" align="left" min-width="150" :show-overflow-tooltip="true"
                        sortable='custom'>
                    </el-table-column>
                    <el-table-column label="操作" fixed="right" width="100">
                        <template slot-scope="scope">
                            <el-button type="text" @click="addOrUpdateHandle(scope.row.id)">{{ scope.row.status !=
                                0 ? "详情" : "编辑" }}</el-button>
                            <el-button type="text" @click="handleDel(scope.row.id)" :disabled="scope.row.status != 0"
                                class='SHZY-table-delBtn'>删除</el-button>
                        </template>
                    </el-table-column>
                </SHZY-table>
                <pagination :total="total" :page.sync="listQuery.currentPage" :limit.sync="listQuery.pageSize"
                    @pagination="initData" />
            </div>
        </div>
        <SHZY-Form v-if="formVisible" ref="SHZYForm" @refresh="refresh" />
        <ExportBox v-if="exportBoxVisible" ref="ExportBox" @download="download" />
        <SHZY-Detail v-if="detailVisible" ref="SHZYDetail" />
    </div>
</template>
<script>
import { getWorkorderList, delWorkorderBatch, delWorkorder, exportData } from '@/api/q1/business/workOrder'
import SHZYForm from './Form'
import ExportBox from './ExportBox'
import SHZYDetail from './Detail'
export default {
    components: { SHZYForm, ExportBox, SHZYDetail },
    name: "business-workOrder",
    data() {
        return {
            query: {
                code: undefined,
                type: undefined,
                status: undefined,
            },
            list: [],
            listLoading: true,
            multipleSelection: [],
            total: 0,
            listQuery: {
                currentPage: 1,
                pageSize: 20,
                sort: "desc",
                sidx: "",
            },
            formVisible: false,
            exportBoxVisible: false,
            detailVisible: false,
            columnList: [
                { prop: 'code', label: '工单号' },
                { prop: 'type', label: '工单类型' },
                { prop: 'materialId', label: '物料' },
                { prop: 'qty', label: '数量' },
                { prop: 'unit', label: '单位' },
                { prop: 'planStartTime', label: '计划开始时间' },
                { prop: 'planEndTime', label: '计划完成时间' },
                { prop: 'actualStartTime', label: '实际开始时间' },
                { prop: 'actualEndTime', label: '实际完成时间' },
                { prop: 'status', label: '状态' },
                { prop: 'remark', label: '备注' },
            ],
            workOrderTypeData: [],
            unitData: [],
            statusData: [
                { 'fullName': "未确认", 'enCode': "0" },
                { 'fullName': "已提交", 'enCode': "1" },
                { 'fullName': "已完工", 'enCode': "2" }
            ]
        }
    },
    computed: {
        menuId() {
            return this.$route.meta.modelId || ''
        }
    },
    created() {
        this.getDictionaryData()
        this.initData()
    },
    methods: {
        getDictionaryData() {
            this.$store.dispatch('base/getDictionaryData', { sort: 'workOrderType' }).then((res) => {
                this.workOrderTypeData = res
            })
            this.$store.dispatch('base/getDictionaryData', { sort: 'MaterialUnit' }).then((res) => {
                this.unitData = res
            })
        },
        getWorkOrderTypeDes(enCode) {
            let workOrderTypeInfo = this.workOrderTypeData.find(x => x.enCode == enCode)
            if (workOrderTypeInfo) {
                return workOrderTypeInfo.fullName
            } else {
                return enCode
            }
        },
        getUnitDes(enCode) {
            let unitInfo = this.unitData.find(x => x.enCode == enCode)
            if (unitInfo) {
                return unitInfo.fullName
            } else {
                return enCode
            }
        },
        sortChange({ column, prop, order }) {
            this.listQuery.sort = order == 'ascending' ? 'asc' : 'desc'
            this.listQuery.sidx = !order ? '' : prop
            this.initData()
        },
        selectHandle(row) {
            if (row.status != 0) {
                return false
            }
            return true
        },
        initData() {
            this.listLoading = true;
            let _query = {
                ...this.listQuery,
                ...this.query,
                menuId: this.menuId,
            };
            let query = {}
            for (let key in _query) {
                if (Array.isArray(_query[key])) {
                    query[key] = _query[key].join()
                } else {
                    query[key] = _query[key]
                }
            }
            getWorkorderList(query).then(res => {
                this.list = res.data.list
                this.total = res.data.pagination.total
                this.listLoading = false
            })
        },
        opendDetail(id) {
            this.detailVisible = true
            this.$nextTick(() => {
                this.$refs.SHZYDetail.init(id, true)
            })
        },
        handleDel(id) {
            this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
                type: 'warning'
            }).then(() => {
                delWorkorder(id).then(res => {
                    this.$message({
                        type: 'success',
                        message: res.msg,
                        onClose: () => {
                            this.initData()
                        }
                    });
                })
            }).catch(() => {
            });
        },
        handleSelectionChange(val) {
            const res = val.map(item => item.id)
            this.multipleSelection = res
        },
        handleBatchRemoveDel() {
            if (!this.multipleSelection.length) {
                this.$message({
                    type: 'error',
                    message: '请选择一条数据',
                    duration: 1500,
                })
                return
            }
            const ids = this.multipleSelection
            this.$confirm('您确定要删除这些数据吗, 是否继续？', '提示', {
                type: 'warning'
            }).then(() => {
                delWorkorderBatch(ids).then(res => {
                    this.$message({
                        type: 'success',
                        message: res.msg,
                        onClose: () => {
                            this.initData()
                        }
                    });
                })
            }).catch(() => { })
        },
        addOrUpdateHandle(id) {
            this.formVisible = true
            this.$nextTick(() => {
                this.$refs.SHZYForm.init(id)
            })
        },
        exportData() {
            this.exportBoxVisible = true
            this.$nextTick(() => {
                this.$refs.ExportBox.init(this.columnList)
            })
        },
        download(data) {
            let query = { ...data, ...this.listQuery, ...this.query, menuId: this.menuId }
            exportData(query).then(res => {
                if (!res.data.url) return
                this.shzy.downloadFile(res.data.url)
                this.$refs.ExportBox.visible = false
                this.exportBoxVisible = false
            })
        },
        search() {
            this.listQuery = {
                currentPage: 1,
                pageSize: 20,
                sort: "desc",
                sidx: "",
            }
            this.initData()
        },
        refresh(isrRefresh) {
            this.formVisible = false
            if (isrRefresh) this.reset()
        },
        reset() {
            for (let key in this.query) {
                this.query[key] = undefined
            }
            this.listQuery = {
                currentPage: 1,
                pageSize: 20,
                sort: "desc",
                sidx: "",
            }
            this.initData()
        }
    }
}
</script>